package com.hd.core.project.module.service;

import com.hd.core.project.module.domain.Select2;

import com.hd.core.project.module.domain.Student;
import com.hd.core.project.module.mapper.StudentMapper;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

/**
 * 学生名单 服务层
 * 
 * <br/>
 * @since JDK 1.8
 * @version  V1.0
 * @author RichardTang 
 * @date: 2018年8月19日
 * @package com.hd.core.project.module.service
 * @copyright:Copyright (c) 2018, 1245811923@qq.com All Rights Reserved.
 */
@Service
public class StudentService extends ServiceImpl<StudentMapper,Student> {
	
	@Autowired
	private ClassesService classesService;
	
	/**
	 * 根据条件实体查询学生<br/>
	 * 
	 * @param entity 条件实体
	 * @return 符合条件的学生数据
	 */
	public List<Student> selectList(Student entity) {
		return baseMapper.selectList(entity);
	}

	/**
	 * 添加一个学生<br/>
	 * 
	 * @param entity 数据实体
	 * @return 是否执行成功
	 */
	@Transactional
	public boolean insert(Student entity) {
		Integer result = 0;
		result = baseMapper.insert(entity);
		result += classesService.increasePeopleNumber(entity.getNowClassId());
		return result > 0;
	}

	/**
	 * <p>删除一个学生</p>
	 * <p>并将对应的班级人数减少</p>
	 * 
	 * @param ids 需要被删除的学生id
	 * @return 受是否执行成功
	 */
	@Transactional
	public boolean delete(List<String> ids) {
		List<String> nowClassIds = baseMapper.selectClassIdById(ids);
		Map<String, Long> map = 
				nowClassIds.stream().collect(Collectors.groupingBy(p -> p,Collectors.counting()));
		classesService.decreasePeopelNumber(map);
		Integer result = baseMapper.deleteBatchIds(ids);
		return result > 0;
	}

	/**
	 * <p>根据条件查询学生</p>
	 * <p>返回Select2格式</p>
	 * 
	 * @param entity 条件实体
	 * @return select2格式数据集合
	 */
	public List<Select2> selectStudentByCondition(Wrapper<Student> entity) {
		return baseMapper.selectStudentByCondition(entity);
	}

	/**
	 * <p>根据id查询学生</p>
	 * 
	 * @param id 目标id
	 * @return 查询到的学生实体
	 */
	public Student selectById(String id) {
		Student student = new Student();
		student.setId(id);
		return baseMapper.selectOne(student);
	}

	/**
	 * <p>根据条件字段更新学生宿舍信息字段的值</p>
	 * <p>单个值判断</p>
	 * 
	 * @param newValue 新值
	 * @param field    字段
	 * @param oldValue 旧值
	 * @return 影响的行数
	 */
	public Integer updateDormByField(Integer newValue, String oldValue,String field) {
		return updateDormByField(newValue,Arrays.asList(oldValue),field);
	}
	
	/**
	 * <p>根据条件字段更新学生宿舍信息字段的值</p>
	 * <p>多个值判断</p>
	 * 
	 * @param newValue 新值
	 * @param field    字段
	 * @param oldValue 旧值
	 * @return 影响的行数
	 */
	@SuppressWarnings("unchecked")
	public Integer updateDormByField(Integer newValue, List<String> oldValue, String field) {
		Student dormInfoCleanCondition = new Student();
		dormInfoCleanCondition.setNowDormitoryId(newValue);
		return baseMapper.update(dormInfoCleanCondition, 
			Condition.create().in(field, oldValue)
		);
	}
}
